import mongoose from 'mongoose'
import Term from '../src/models/terms'
import Question from '../src/models/questions'

import config from '../config/'
let { ObjectId } = mongoose.Types
mongoose.Promise = global.Promise
mongoose.connect(config.database);

// ----------查询例子
  async function test1() {
    console.log('开始查询');
    const terms=await Term.find({}).exec()
    console.log(terms,'结束查询');
    return false;
  }
// ----------------添加数据

function createData() {
  let _newId=new ObjectId
  let term={
    _id:_newId,
    title:'这期有对对应的问题',
    questions:[_newId]
  }
  let question={
    _id: _newId,
    question: '测试1',
    tips:'这是问题的摘要',
    logoSrc:'',
    answers:['答案1','答案2'],
    trueAnswers:[0],
    status: true
  }
  Term.create(term,function (err,doc) {
      if(err){console.log(err)}else{
        console.log(doc);
      }
  })
  Question.create(question,function (err,doc) {
      if(err){console.log(err)}else{
        console.log(doc);
      }
  })
}
// createData()
Term.findOne({title:/有/i})
    .populate('questions')
    .exec(function (err,doc) {
      if(err){console.log(err)}else{
        console.log(doc);
      }
    })
